

 file open fh using .\Results\Table_Happy.tex, write replace 

 file write fh "\begin{center}" _n ///
	"{\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}" _n ///
	"\begin{tabular}{l*{7}{c}}" _n ///
	"\toprule \toprule" _n ///
	"& (1) & (2) & (3) & (4) & (5) & (6)  \\" _n ///
	"  & \multicolumn{3}{c}{Happy yesterday} & \multicolumn{3}{c}{Life worthwhile} \\ "_n ///
	"\cline{2-7}" _n
	
	local model happy
	local col = 6

* White*Male
	forval i = 1/`col' {
if `i'==1 { 
	est restore `model'`i'
	local var WhiteMale
	local b= string(_b[`var'], "%5.3f")
	* We need to recover the stat significance. The commands below do so.
	if 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) >.1 { 
		local star= ""    
		}
		if 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) >.05 & 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) <=.1  {
		local star = "$^{*}$"   
		}
		if 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) >.01 & 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) <=.05  { 
		local star = "$^{**}$" 
		}
		if 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) <=.01  {
		local star = "$^{***}$" 
		}
	file write fh "White Male & `b'`star'" 
}
if `i'>1 & `i'<`col' {
	est restore `model'`i'
	local b= string(_b[`var'], "%5.3f")
	* We need to recover the stat significance. The commands below do so.
	if 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) >.1 { 
		local star= ""    
		}
		if 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) >.05 & 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) <=.1  {
		local star = "$^{*}$"   
		}
		if 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) >.01 & 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) <=.05  { 
		local star = "$^{**}$" 
		}
		if 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) <=.01  {
		local star = "$^{***}$" 
		}
	file write fh "& `b'`star'" 
}
if `i'==`col' {	
	est restore `model'`i'
	local b= string(_b[`var'], "%5.3f")
	* We need to recover the stat significance. The commands below do so.
	if 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) >.1 { 
		local star= ""    
		}
		if 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) >.05 & 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) <=.1  {
		local star = "$^{*}$"   
		}
		if 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) >.01 & 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) <=.05  { 
		local star = "$^{**}$" 
		}
		if 2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])) <=.01  {
		local star = "$^{***}$" 
		}
	file write fh "& `b'`star' \\" _n
	}
	}

* Second write p-values
	forval i = 1/`col' {
if `i'==1 { 
	est restore `model'`i'
	local pval= string(2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])), "%5.3f")
	file write fh " & (`pval')" 
}
if `i' >1 & `i'<`col' {
	est restore `model'`i'
	local pval= string(2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])), "%5.3f")
	file write fh "& (`pval')" 
}
if `i' ==`col' {
	est restore `model'`i'
	local pval= string(2*t(`e(df_r)',-abs(_b[`var']/_se[`var'])), "%5.3f")
	file write fh "& (`pval') " _n
}
	}	
	
	
	
	* The commands below complete the document
file write fh  "\\ \midrule" _n  ///
				"Sample &          All       &   All  & White & All & All & White \\[0.4ex]" _n 
* Adding mean
	forval i = 1/`col' {
if `i'==1 { 
	est restore `model'`i'
	local mean= string(`e(mean)', "%5.2f")
	file write fh "Mean dep variable      & `mean'" 
}
if `i'>1 & `i'<`col' {
	est restore `model'`i'
	local mean= string(`e(mean)', "%5.2f")
	file write fh "& `mean'" 
}
if `i'==`col' {
	est restore `model'`i'
	local mean= string(`e(mean)', "%5.2f")
	file write fh "& `mean' \\[0.4ex] " _n
}
	}
			
	file write fh			"Individual controls &                   &   $\checkmark$    & $\checkmark$         &     & $\checkmark$  & $\checkmark$        \\[0.4ex]" _n
* Adding observations
	forval i = 1/`col' {
if `i'==1 { 
	est restore `model'`i'
	local N= string(`e(N)', "%4.0f")
	file write fh "N.obs      & `N'" 
}
if `i'>1 & `i'<`col' {
	est restore `model'`i'
	local N= string(`e(N)', "%4.0f")
	file write fh "& `N'" 
}
if `i'==`col' {
	est restore `model'`i'
	local N= string(`e(N)', "%4.0f")
	file write fh "& `N' \\ \bottomrule \bottomrule " _n
}
	}
				
file write fh		"\end{tabular}}" _n 
file write fh		"\end{center}" _n 
file close fh

